home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************
-
- Gnucleus - A node application for the Gnutella network
- Copyright (C) 2000 John Marshall
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
- For support, questions, comments, etc...
- E-Mail:
- swabby@c0re.net
-
- Address:
- 21 Cadogan Way
- Nashua, NH, USA 03062
-
- ********************************************************************************/
-
- // ViewConnect.cpp : implementation of the CViewConnect class
- //
-
- #include "stdafx.h"
- #include "Gnucleus.h"
-
- #include "GnucleusDoc.h"
- #include "ViewConnect.h"
- #include "ViewSearch.h"
- #include "MainFrm.h"
-
- #include "GnuSock.h"
- #include "GnuHash.h"
- #include "GnuControl.h"
-
- #include "IPFilter.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CViewConnect
-
- IMPLEMENT_DYNCREATE(CViewConnect, CFormView)
-
- BEGIN_MESSAGE_MAP(CViewConnect, CFormView)
- //{{AFX_MSG_MAP(CViewConnect)
- ON_WM_SIZE()
- ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
- ON_NOTIFY(NM_DBLCLK, IDC_LST_CONNECTED, OnDblclkLstConnected)
- ON_NOTIFY(NM_DBLCLK, IDC_LST_CACHE, OnDblclkLstCache)
- ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
- ON_EN_CHANGE(IDC_EDIT_PORT, OnChangeEditPort)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CViewConnect construction/destruction
-
- CViewConnect::CViewConnect()
- : CFormView(CViewConnect::IDD)
- {
- //{{AFX_DATA_INIT(CViewConnect)
- //}}AFX_DATA_INIT
-
- btnSearch = NULL;
- cbKeyword = NULL;
- cbSpeed = NULL;
- }
-
- CViewConnect::~CViewConnect()
- {
- //KillTimer(UpdateTimer);
- }
-
- void CViewConnect::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CViewConnect)
- DDX_Control(pDX, IDC_COMBO_HOST, m_cbxHost);
- DDX_Control(pDX, IDC_BUTTON_ADD, m_btnAdd);
- DDX_Control(pDX, IDC_BUTTON_REMOVE, m_btnRemove);
- DDX_Control(pDX, IDC_EDIT_PORT, m_ebPort);
- DDX_Control(pDX, IDC_STATIC_CONNECT, m_stcConnect);
- DDX_Control(pDX, IDC_LST_CACHE, m_lstCache);
- DDX_Control(pDX, IDC_LST_CONNECTED, m_lstConnected);
- DDX_Control(pDX, IDC_STATIC_ADD, m_stcAdd);
- DDX_Control(pDX, IDC_STATIC_EXIST, m_stcExist);
- DDX_Control(pDX, IDC_STATIC_HOST, m_stcHost);
- DDX_Control(pDX, IDC_STATIC_PORT, m_stcPort);
- //}}AFX_DATA_MAP
- }
-
- BOOL CViewConnect::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CFormView::PreCreateWindow(cs);
- }
-
- void CViewConnect::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- ResizeParentToFit();
-
- // Initialize the list control
- int offSet;
-
- if(m_lstConnected.GetScrollLimit(SB_VERT))
- offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
- else
- offSet = 4;
-
- CRect rect;
- m_lstConnected.GetWindowRect(&rect);
-
- m_lstConnected.InsertColumn(0, "Node", LVCFMT_LEFT,
- (rect.Width() - offSet) * 2/5, 0);
- m_lstConnected.InsertColumn(1, "Port", LVCFMT_LEFT,
- (rect.Width() - offSet) * 1/5, 1);
- m_lstConnected.InsertColumn( 2, "Status", LVCFMT_LEFT,
- (rect.Width() - offSet) * 2/5, 2);
-
- m_lstConnected.SetExtendedStyle(LVS_EX_FULLROWSELECT);
-
-
- if(m_lstCache.GetScrollLimit(SB_VERT))
- offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
- else
- offSet = 4;
-
- m_lstCache.GetWindowRect(&rect);
-
- m_lstCache.InsertColumn(0, "Node", LVCFMT_LEFT,
- (rect.Width() - offSet) * 3/5, 0);
- m_lstCache.InsertColumn(1, "Port", LVCFMT_RIGHT,
- (rect.Width() - offSet) * 1/5, 1);
- m_lstCache.InsertColumn(2, "Ping", LVCFMT_RIGHT,
- (rect.Width() - offSet) * 1/5, 2);
-
- m_lstCache.SetExtendedStyle(LVS_EX_FULLROWSELECT);
-
-
- // Fill cache with saved hosts
- int index, posLst;
- CString HostPort, Host, Port;
- POSITION pos = GetDocument()->SavedHosts.GetHeadPosition();
-
- for (int i = 0; i < GetDocument()->SavedHosts.GetCount();i++)
- {
- HostPort = GetDocument()->SavedHosts.GetAt(pos);
-
- index = HostPort.Find(":", 0);
- Host = HostPort.Mid(0, index);
- Port = HostPort.Mid(index + 1, HostPort.GetLength() - index);
-
- posLst = m_lstCache.GetItemCount();
- m_lstCache.InsertItem(posLst, "");
- m_lstCache.SetItemText(posLst, m_lstCache.GetColumnNumber( "Node" ), Host);
- m_lstCache.SetItemText(posLst, m_lstCache.GetColumnNumber( "Port" ), Port);
-
- GetDocument()->SavedHosts.GetNext(pos);
- }
-
- m_cbxHost.AddString(_T("gnet.ath.cx"));
- m_cbxHost.AddString(_T("gnet2.ath.cx"));
- m_cbxHost.AddString(_T("gnet3.ath.cx"));
- m_cbxHost.AddString(_T("gnet4.ath.cx"));
- m_cbxHost.AddString(_T("gnet5.ath.cx"));
- // Set Buttons and edit boxes
- //btnSearch = (CButton *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_BUTTON_SEARCH);
- //cbKeyword = (CComboBox *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SEARCH);
- //cbSpeed = (CComboBox *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SPEED);
-
- //btnSearch->EnableWindow(FALSE);
- //cbKeyword->EnableWindow(FALSE);
- //cbSpeed->EnableWindow(FALSE);
-
- m_btnRemove.EnableWindow(FALSE);
- m_ebPort.SetWindowText("6346");
-
- // Start the timer
- UpdateTimer = SetTimer(1, 500, 0);
- oneSec = 1;
-
- GetParentFrame()->GetClientRect(&rect);
- OnSize(SIZE_RESTORED, rect.right - 4, rect.bottom - 4);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CViewConnect diagnostics
-
- #ifdef _DEBUG
- void CViewConnect::AssertValid() const
- {
- CFormView::AssertValid();
- }
-
- void CViewConnect::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
-
- CGnucleusDoc* CViewConnect::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGnucleusDoc)));
- return (CGnucleusDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CViewConnect message handlers
-
- // Memory leaks when shutting down soon after starting.
- // CViewConnect::OnSize and CViewSearch::OnSize and CViewTransfer::OnSize should have
- // the LPRECT vars changes to RECT var to avoid allocation with "new". This keeps
- // memory leaks from occuring in this code. Here is the code to save you some time.
-
- void CViewConnect::OnSize(UINT nType, int cx, int cy)
- // leave all hope behind those who enter here
- {
- if( cx < 580 || cy < 266) // too small and formatting goes to hell.
- ; // so just ignore the size change.
-
- else if(m_lstCache.m_hWnd != NULL)
- {
- int top_LstCache, left_LstCache,
- top_LstConnected, left_LstConnected,
- top_StcConnect, left_StcConnect, right_StcConnect, bottom_StcConnect,
- top_BtnRemove, left_BtnRemove, right_BtnRemove, bottom_BtnRemove,
- top_BtnAdd, left_BtnAdd, right_BtnAdd, bottom_BtnAdd,
- top_StcAdd, left_StcAdd, right_StcAdd, bottom_StcAdd,
- top_StcHost, left_StcHost, right_StcHost, bottom_StcHost,
- top_StcPort, left_StcPort, right_StcPort, bottom_StcPort,
- top_cbxHost, left_cbxHost, right_cbxHost, bottom_cbxHost,
- top_EbPort, left_EbPort, right_EbPort, bottom_EbPort;
-
- // I'm scared
- // DW - new was being used - caused memory leaks
- RECT rect_Wnd,
- rect_LstCache,
- rect_LstConnected,
- rect_StcConnect,
- rect_BtnRemove,
- rect_BtnAdd,
- rect_StcAdd,
- rect_StcHost,
- rect_StcPort,
- rect_cbxHost,
- rect_EbPort;
-
- GetWindowRect(&rect_Wnd);
- m_lstCache.GetWindowRect(&rect_LstCache);
- m_lstConnected.GetWindowRect(&rect_LstConnected);
- m_stcConnect.GetWindowRect(&rect_StcConnect);
- m_btnRemove.GetWindowRect(&rect_BtnRemove);
- m_btnAdd.GetWindowRect(&rect_BtnAdd);
- m_stcAdd.GetWindowRect(&rect_StcAdd);
- m_stcHost.GetWindowRect(&rect_StcHost);
- m_stcPort.GetWindowRect(&rect_StcPort);
- m_cbxHost.GetWindowRect(&rect_cbxHost);
- m_ebPort.GetWindowRect(&rect_EbPort);
-
- // oh god, please no
- top_LstCache = rect_LstCache.top - rect_Wnd.top - 2;
- left_LstCache = rect_LstCache.left - rect_Wnd.left - 2;
-
- top_LstConnected = rect_LstConnected.top - rect_Wnd.top - 2;
- left_LstConnected = rect_LstConnected.left - rect_Wnd.left - 2;
-
- top_StcConnect = rect_StcConnect.top - rect_Wnd.top - 2;
- left_StcConnect = rect_StcConnect.left - rect_Wnd.left - 2;
- right_StcConnect = rect_StcConnect.right - rect_StcConnect.left;
- bottom_StcConnect = rect_StcConnect.bottom - rect_StcConnect.top;
-
- top_BtnRemove = rect_BtnRemove.top - rect_Wnd.top - 2;
- left_BtnRemove = rect_BtnRemove.left - rect_Wnd.left - 2;
- right_BtnRemove = rect_BtnRemove.right - rect_BtnRemove.left;
- bottom_BtnRemove = rect_BtnRemove.bottom - rect_BtnRemove.top;
-
- top_BtnAdd = rect_BtnAdd.top - rect_Wnd.top - 2;
- left_BtnAdd = rect_BtnAdd.left - rect_Wnd.left - 2;
- right_BtnAdd = rect_BtnAdd.right - rect_BtnAdd.left;
- bottom_BtnAdd = rect_BtnAdd.bottom - rect_BtnAdd.top;
-
- // it's coming back...
- top_StcAdd = rect_StcAdd.top - rect_Wnd.top - 2;
- left_StcAdd = rect_StcAdd.left - rect_Wnd.left - 2;
- right_StcAdd = rect_StcAdd.right - rect_StcAdd.left;
- bottom_StcAdd = rect_StcAdd.bottom - rect_StcAdd.top;
-
- top_StcHost = rect_StcHost.top - rect_Wnd.top - 2;
- left_StcHost = rect_StcHost.left - rect_Wnd.left - 2;
- right_StcHost = rect_StcHost.right - rect_StcHost.left;
- bottom_StcHost = rect_StcHost.bottom - rect_StcHost.top;
-
- top_StcPort = rect_StcPort.top - rect_Wnd.top - 2;
- left_StcPort = rect_StcPort.left - rect_Wnd.left - 2;
- right_StcPort = rect_StcPort.right - rect_StcPort.left;
- bottom_StcPort = rect_StcPort.bottom - rect_StcPort.top;
-
- top_cbxHost = rect_cbxHost.top - rect_Wnd.top - 2;
- left_cbxHost = rect_cbxHost.left - rect_Wnd.left - 2;
- right_cbxHost = rect_cbxHost.right - rect_cbxHost.left;
- bottom_cbxHost = rect_cbxHost.bottom - rect_cbxHost.top;
-
- top_EbPort = rect_EbPort.top - rect_Wnd.top - 2;
- left_EbPort = rect_EbPort.left - rect_Wnd.left - 2;
- right_EbPort = rect_EbPort.right - rect_EbPort.left;
- bottom_EbPort = rect_EbPort.bottom - rect_EbPort.top;
-
- // the horrors you've witnessed, the horrors you will now
- m_lstCache.MoveWindow(left_LstCache, top_LstCache, cx / 2 - 10, cy - top_LstCache - bottom_BtnAdd - bottom_StcAdd - 30);
- m_lstConnected.MoveWindow(cx / 2 + 10, top_LstCache, cx / 2 - 17, cy - top_LstConnected - bottom_BtnRemove - 21);
- m_stcConnect.MoveWindow(cx / 2 + 10, top_StcConnect, right_StcConnect, bottom_StcConnect);
- m_btnRemove.MoveWindow(cx / 4 * 3 - (right_BtnRemove / 2), cy - bottom_BtnRemove - 7, right_BtnRemove, bottom_BtnRemove);
- m_btnAdd.MoveWindow(cx / 4 - (right_BtnAdd / 2), cy - bottom_BtnAdd - 7, right_BtnAdd, bottom_BtnAdd);
- m_stcAdd.MoveWindow(left_StcAdd, cy - bottom_BtnAdd - bottom_StcAdd - 22, cx / 2 - 10, bottom_StcAdd);
- m_stcHost.MoveWindow(left_StcHost, cy - bottom_BtnAdd - bottom_StcAdd - 13 + bottom_StcAdd / 5, right_StcHost, bottom_StcHost);
- m_stcPort.MoveWindow(left_StcPort, cy - bottom_BtnAdd - bottom_StcAdd - 18 + 3 * bottom_StcAdd / 5, right_StcPort, bottom_StcPort);
- m_cbxHost.MoveWindow(left_cbxHost, cy - bottom_BtnAdd - bottom_StcAdd - 13 + bottom_StcAdd / 5, (cx / 2 - 10) / 2, bottom_cbxHost);
- m_ebPort.MoveWindow(left_EbPort, cy - bottom_BtnAdd - bottom_StcAdd - 18 + 3 * bottom_StcAdd / 5, right_EbPort, bottom_EbPort);
-
- // the darkness has put out the light
-
- int offSet;
-
- if(m_lstConnected.GetScrollLimit(SB_VERT))
- offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
- else
- offSet = 4;
-
- CRect rect;
- m_lstConnected.GetWindowRect(&rect);
-
- m_lstConnected.SetColumnWidth(0, (rect.Width() - offSet) * 2/5);
- m_lstConnected.SetColumnWidth(1, (rect.Width() - offSet) * 1/5);
- m_lstConnected.SetColumnWidth(2, (rect.Width() - offSet) * 2/5);
-
- if(m_lstCache.GetScrollLimit(SB_VERT))
- offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
- else
- offSet = 4;
-
- m_lstCache.GetWindowRect(&rect);
-
- m_lstCache.SetColumnWidth(0, (rect.Width() - offSet) * 3/5);
- m_lstCache.SetColumnWidth(1, (rect.Width() - offSet) * 1/5);
- m_lstCache.SetColumnWidth(2, (rect.Width() - offSet) * 1/5);
- }
-
- CFormView::OnSize(nType, cx, cy);
- }
-
-
- void CViewConnect::OnButtonAdd()
- {
- CString Host, Port;
- int index = -1;
-
- m_cbxHost.GetWindowText(Host);
- //m_ebHost.GetWindowText(Host);
- m_ebPort.GetWindowText(Port);
-
- // accept a host:port format.
- if( (index = Host.Find(_T(":"), 0)) != -1)
- {
- Port = Host.Mid(index + 1, Host.GetLength() - index);
- Host = Host.Mid(0, index);
-
- //m_ebHost.SetWindowText(Host);
- m_cbxHost.SetWindowText(Host);
- m_ebPort.SetWindowText(Port);
- }
- if(Host == "" || Port == "")
- return;
-
- AddConnect(Host, Port);
- }
-
- BOOL CViewConnect::AddConnect(CString Host, CString Port)
- {
- for(int i = 0; i < m_lstConnected.GetItemCount(); i++)
- {
- if(m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber("Node" ) ) == Host &&
- m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber("Port" ) ) == Port)
- {
- //AfxMessageBox("Host already connected.");
- return FALSE; // DW return value
- }
- }
-
- int pos = m_lstConnected.GetItemCount();
- m_lstConnected.InsertItem(pos, "");
- m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Node" ), Host);
- m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Port" ), Port);
- m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Status" ), "Connecting");
-
- // DW return new BOOL result
- return GetDocument()->GnuComm->AddNode(Host, Port);
- }
-
-
- void CViewConnect::OnButtonRemove()
- {
- int nItem;
- CString Host, Port;
- POSITION pos = m_lstConnected.GetFirstSelectedItemPosition();
-
- CString Status = "Disconnecting";
-
- while(pos != NULL)
- {
- nItem = m_lstConnected.GetNextSelectedItem(pos);
-
- Host = m_lstConnected.GetItemText(nItem, m_lstConnected.GetColumnNumber("Node" ) );
- Port = m_lstConnected.GetItemText(nItem, m_lstConnected.GetColumnNumber("Port" ) );
-
- GetDocument()->QueueDisconnect.AddTail(Host + ":" + Port);
-
- m_lstConnected.SetItemText(nItem, m_lstConnected.GetColumnNumber( "Status"), Status);
- }
-
- if(m_lstConnected.GetItemCount() == 0)
- {
- //btnSearch->EnableWindow(FALSE);
- //cbKeyword->EnableWindow(FALSE);
- //cbSpeed->EnableWindow(FALSE);
-
- m_btnRemove.EnableWindow(FALSE);
- }
- }
-
- // ViewConnect.cpp - OnTimer call should not be re-entrant
- void CViewConnect::OnTimer(UINT nIDEvent)
- {
- int index;
- CString Host, Port, HostPort, Status;
- // DW - Not re-entrant
- static bool bInFunc = FALSE;
-
- CFormView::OnTimer(nIDEvent);
-
- if (bInFunc)
- return;
-
- bInFunc = TRUE;
-
-
- oneSec = oneSec % 1 ? 1 : 2;
-
- //btnSearch = (CButton *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_BUTTON_SEARCH);
- //cbKeyword = (CComboBox *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SEARCH);
- //cbSpeed = (CComboBox *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SPEED);
-
-
- if(nIDEvent == UpdateTimer)
- {
-
- // Update Connects
- while(!GetDocument()->QueueConnect.IsEmpty())
- {
- // Break the string into two parts
- HostPort = GetDocument()->QueueConnect.RemoveHead();
-
- index = HostPort.Find(":", 0);
- Host = HostPort.Mid(0, index);
- Port = HostPort.Mid(index + 1, HostPort.GetLength() - index);
-
- for(int i = 0; i < m_lstConnected.GetItemCount(); i++)
- {
- // Connecting and outgoing node
- if(m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Node" ) ) == Host &&
- m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Port" ) ) == Port)
- {
- m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber("Status" ), "Connected");
- }
- }
- // Connecting an incomming node
- if(Port == "Inbound")
- {
- int pos = m_lstConnected.GetItemCount();
- m_lstConnected.InsertItem(pos, "" );
- m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Node" ) , Host);
- m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Port" ) , Port);
- m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Status" ) , "Connected");
- }
-
- // Send existing search queries out to the newly connected node
- // FIXME, it doesnt work =)
- CString Title;
- POSITION pos = GetDocument()->GetFirstViewPosition();
-
- while (pos != NULL)
- {
- CView* pView = GetDocument()->GetNextView(pos);
- pView->GetParentFrame()->GetWindowText(Title);
-
- if(Title.Mid(0, 9) == "Searching")
- {
- CGnuSock *node = GetDocument()->GnuComm->GetNode(HostPort);
- node->Send_Query( (CViewSearch *) pView);
- }
- }
-
- // Update the host count
- GetDocument()->TotalHosts = GetDocument()->Connections.GetCount();
-
- CGnuSock *newNode = GetDocument()->GnuComm->GetNode(HostPort);
-
- if(newNode != NULL)
- newNode->RefreshHostCount();
- }
-
- // Update nodes Connecting
- CString MaxDots;
- for(int i = 0; i < GetDocument()->m_TimeoutConnect; i++)
- MaxDots += '.';
-
- for(i = 0; i < m_lstConnected.GetItemCount(); i++)
- {
- Status = m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) );
-
- Host = m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Node" ) );
- Port = m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Port" ) );
-
- if(Status.Find("Connecting") != -1)
- {
- if(Status.Find(MaxDots) != -1)
- {
- CGnuSock* sock = GetDocument()->GnuComm->GetNode (CString(Host + ":" + Port));
-
- if (sock)
- {
- Status = "Disconnecting";
- m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) , Status);
- sock->WantToDisconnect();
-
-
- if (sock->m_CanRelease)
- {
- GetDocument()->GnuComm->RemoveNode(Host, Port);
-
- sock = NULL;
- }
- }
-
- if (!sock)
- {
- m_lstConnected.DeleteItem(i);
- }
- }
- else
- {
- if(oneSec == 2)
- Status += ".";
- m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) , Status);
- }
- }
- // well it's not connect, so it must be:
- else if(Status.Find("Disconnecting") != -1)
- {
- CGnuSock* sock = GetDocument()->GnuComm->GetNode (Host + ":" + Port);
-
- if(sock)
- {
- sock->WantToDisconnect();
-
- // If it's been this long and there has been no OnClose, then kill it anyways.
- // or if it has been "released" as in shutdown now!
- if (sock->m_CanRelease || Status.Find(".") != -1)
- {
- GetDocument()->GnuComm->RemoveNode(Host, Port);
-
- sock = NULL;
- }
- }
- // if the socket was invalid to beginwith, or has been killed, then remove from list.
- if (!sock) // not else, because sock can change
- {
- m_lstConnected.DeleteItem(i);
- continue;
- }
-
- // Update the exclamation marks.
- if(oneSec == 2)
- {
- Status += ".";
- m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) , Status);
- }
- }
-
- }
-
- // Update Disconnects. This queue gets added to by CleanUp
- while(!GetDocument()->QueueDisconnect.IsEmpty())
- {
- // Break the string into two parts
- HostPort = GetDocument()->QueueDisconnect.RemoveHead();
-
- index = HostPort.Find(":", 0);
- Host = HostPort.Mid(0, index);
- Port = HostPort.Mid(index + 1, HostPort.GetLength() - index);
-
- CGnuSock* node = GetDocument()->GnuComm->GetNode (HostPort);
-
- for(int i = 0; i < m_lstConnected.GetItemCount(); i++)
- {
- if(m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Node" ) ) == Host &&
- m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Port" ) ) == Port)
- {
- if(node && !node->m_CanRelease ) // change text, and let loop above get it.
- {
- CString Status = "Disconnecting";
- m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) , Status);
- }
- else // invalid, so delete it
- {
- GetDocument()->GnuComm->RemoveNode (Host, Port);
- m_lstConnected.DeleteItem(i);
- }
- break;
- }
- }
-
- // Update the host count
- GetDocument()->TotalHosts = GetDocument()->Connections.GetCount();
-
- }
-
- GetDocument()->RemoveEarliestHostPort(&HostPort);
- if (!HostPort.IsEmpty())
- {
- // Update the Host cache
- // if(!GetDocument()->QueueCache.IsEmpty())
- // {
- // HostPort = GetDocument()->QueueCache.RemoveTail();
-
- index = HostPort.Find(":", 0);
- Host = HostPort.Mid(0, index);
- Port = HostPort.Mid(index + 1, HostPort.GetLength() - index);
-
- // See if its a duplicate
- LVFINDINFO *findInfo = new LVFINDINFO;
-
- findInfo->flags = LVFI_STRING;
- findInfo->psz = Host;
-
- if(m_lstCache.FindItem(findInfo) == -1 &&
- CIPFilter::AllowIP( StrtoIP(Host) ) &&
- CIPFilter::IsPrivateIP(StrtoIP(Host), StrtoIP(Host)) == 0 )
- {
- // Insert the item
- m_lstCache.InsertItem(0, "");
- m_lstCache.SetItemText(0, m_lstCache.GetColumnNumber( "Node" ) , Host);
- m_lstCache.SetItemText(0, m_lstCache.GetColumnNumber( "Port" ) , Port);
-
- if(m_lstCache.GetItemCount() > 500)
- m_lstCache.DeleteItem(500);
- }
-
- delete findInfo;
- }
-
- while(m_lstCache.GetItemCount() > 500)
- m_lstCache.DeleteItem(500);
-
- // Do some 'intelligent' node management
- if(GetDocument()->m_MonitorType != 0)
- {
- int connects = GetDocument()->Connections.GetCount(),
- optimal = GetDocument()->m_ConnectNum;
-
- if(GetDocument()->m_MonitorType == 1)
- if(connects >= optimal)
- RemoveFirstNode();
- if(GetDocument()->m_MonitorType == 2)
- {
- if(connects > optimal)
- RemoveFirstNode();
- if(connects < optimal)
- if (!ConnectRandomNode()) // DW - No need to do rest if no connect
- {
- bInFunc = FALSE;
- return;
- }
- }
-
- if(GetDocument()->m_MonitorType == 3)
- if(connects <= optimal)
- if (!ConnectRandomNode()) // DW - No need to do rest if no connect
- {
- bInFunc = FALSE;
- return;
- }
- }
-
- CString Title = "Connected to ";
- Title += CommaIze(DWrdtoStr( GetDocument()->GnuComm->GetHostCount()) );
- Title += " Nodes";
-
- GetParentFrame()->SetWindowText(Title);
-
- if(m_lstConnected.GetItemCount())
- {
- //btnSearch->EnableWindow();
- //cbKeyword->EnableWindow();
- //cbSpeed->EnableWindow();
-
- m_btnRemove.EnableWindow();
- }
- else
- {
- //btnSearch->EnableWindow(FALSE);
- //cbKeyword->EnableWindow(FALSE);
- //cbSpeed->EnableWindow(FALSE);
-
- m_btnRemove.EnableWindow(FALSE);
- }
- }
-
- bInFunc = FALSE;
- }
-
- void CViewConnect::OnDblclkLstConnected(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMITEMACTIVATE Control = (LPNMITEMACTIVATE) pNMHDR;
-
- int nItem = m_lstConnected.HitTest(Control->ptAction);
- CString Host, Port, HostPort, ViewHostPort, Title = "Node ";
-
- if(nItem != -1)
- {
- Host = m_lstConnected.GetItemText(nItem, m_lstConnected.GetColumnNumber( "Node" ) );
- Port = m_lstConnected.GetItemText(nItem, m_lstConnected.GetColumnNumber( "Port" ) );
- HostPort = Host;
- HostPort += ":";
- HostPort += Port;
- Title += HostPort;
-
- POSITION pos = GetDocument()->GetFirstViewPosition();
-
- // Make sure window does not already exist
- while (pos != NULL)
- {
- CView* pView = GetDocument()->GetNextView(pos);
-
- pView->GetParentFrame()->GetWindowText(ViewHostPort);
-
- if(ViewHostPort == Title)
- {
- pView->GetParentFrame()->BringWindowToTop();
-
- return;
- }
- }
-
- CFrameWnd* pNewFrame = ((CGnucleusApp *) AfxGetApp())->m_pNodeViewTemplate->CreateNewFrame(((CGnucleusApp *) AfxGetApp())->MasterDoc, NULL);
-
- pNewFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
- pNewFrame->SetWindowText(Title);
-
- ((CGnucleusApp *) AfxGetApp())->m_pNodeViewTemplate->InitialUpdateFrame(pNewFrame, ((CGnucleusApp *) AfxGetApp())->MasterDoc);
- }
-
- *pResult = 0;
- }
-
- void CViewConnect::OnDblclkLstCache(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMITEMACTIVATE Control = (LPNMITEMACTIVATE) pNMHDR;
-
- int nItem = m_lstCache.HitTest(Control->ptAction);
- CString Host, Port;
-
- if(nItem != -1)
- {
- Host = m_lstCache.GetItemText(nItem, m_lstCache.GetColumnNumber( "Node" ) );
- Port = m_lstCache.GetItemText(nItem, m_lstCache.GetColumnNumber( "Port" ) );
-
- AddConnect(Host, Port);
- }
-
- *pResult = 0;
- }
-
- BOOL CViewConnect::ConnectRandomNode()
- {
- if(m_lstCache.GetItemCount())
- {
- int nItem = rand() % m_lstCache.GetItemCount();
-
- CString Host = m_lstCache.GetItemText(nItem, m_lstCache.GetColumnNumber( "Node" ) );
- CString Port = m_lstCache.GetItemText(nItem, m_lstCache.GetColumnNumber( "Port" ) );
-
- if( CIPFilter::AllowIP( StrtoIP(Host) ) &&
- (CIPFilter::IsPrivateIP(StrtoIP(Host), StrtoIP(Host)) & (0x2|0x4)) == 0) // if 0 or 1
- return AddConnect(Host, Port); // DW
- }
- return FALSE; // DW
- }
-
-
- void CViewConnect::RemoveFirstNode()
- {
- CString str_status = "Disconnecting";
-
- if( m_lstConnected.GetItemText(0, m_lstConnected.GetColumnNumber( "Status" ) ).Find(str_status) == -1)
- m_lstConnected.SetItemText(0, m_lstConnected.GetColumnNumber( "Status" ) , str_status);
- }
-
-
-
- void CViewConnect::OnEditPaste()
- {
- // TODO: Add your command handler code here
-
- }
-
- void CViewConnect::OnChangeEditPort()
- {
- // TODO: If this is a RICHEDIT control, the control will not
- // send this notification unless you override the CFormView::OnInitDialog()
- // function and call CRichEditCtrl().SetEventMask()
- // with the ENM_CHANGE flag ORed into the mask.
-
- // TODO: Add your control notification handler code here
-
- }
-